<template>
    <div class="tag-container">
        <!-- 添加标签的输入和按钮 -->
        <div class="add-tag">
            <input type="text" v-model="newTag" placeholder="输入标签" @keyup.enter="addTag">
            <button @click="addTag">添加标签</button>
        </div>

        <!-- 标签展示区域 -->
        <div class="tags-list">
            <div v-for="tag in tags" :key="tag.id" class="tag-item">
                <span>{{ tag.text }}</span>
                <button class="delete-btn" @click="removeTag(tag.id)">×</button>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            newTag: '',
            tags: [], // 存储标签数组
            nextId: 1   // 用于生成唯一ID
        }
    },
    methods: {
        addTag() {
            if (this.newTag.trim()) {
                this.tags.push({
                    id: this.nextId++,
                    text: this.newTag.trim()
                })
                this.newTag = '' // 清空输入框
            }
        },
        removeTag(tagId) {
            this.tags = this.tags.filter(tag => tag.id !== tagId)
        }
    }
}
</script>

<style scoped>
.tag-container {
    max-width: 600px;
    margin: 20px auto;
    padding: 20px;
}

.add-tag {
    margin-bottom: 20px;
    display: flex;
    gap: 10px;
}

input {
    padding: 8px;
    flex: 1;
    border: 1px solid #ccc;
    border-radius: 4px;
}

button {
    padding: 8px 16px;
    background: #409eff;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

button:hover {
    background: #66b1ff;
}

.tags-list {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.tag-item {
    display: flex;
    align-items: center;
    background: #f0f2f5;
    padding: 5px 12px;
    border-radius: 4px;
    font-size: 14px;
}

.delete-btn {
    margin-left: 8px;
    padding: 0;
    width: 18px;
    height: 18px;
    line-height: 18px;
    background: #c0c4cc;
    font-size: 14px;
}

.delete-btn:hover {
    background: #909399;
}
</style>